import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created with IntelliJ IDEA.
 * User: Станислав
 * Date: 03.02.14
 * Time: 11:24
 * To change this template use File | Settings | File Templates.
 */
public class SpinnoFrame {
    JFrame frame;
    int x = 460;
    int y = 170;
    double angle = 0;
    Dimension screenSize;
    Image img = Toolkit.getDefaultToolkit().getImage("src\\image\\1.jpeg");

    public SpinnoFrame() {

        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame = new JFrame("Spin Frame");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(350,250);


        Container content = frame.getContentPane();


        JPanel userPaneX = new JPanel(new BorderLayout());
        JLabel userLabeX = new JLabel("X: " + x);
        JTextField TextFieldX = new JTextField();
        // знать как добавить Х,У;
        userLabeX.setLabelFor(TextFieldX);
        userPaneX.add(userLabeX, BorderLayout.WEST);
        userPaneX.add(TextFieldX, BorderLayout.CENTER);

        JPanel userPaneY = new JPanel(new BorderLayout());
        JLabel userLabeY = new JLabel("Y: " + y);
        userPaneY.add(userLabeY, BorderLayout.WEST);


        JPanel userPaneCos = new JPanel(new BorderLayout());
        JLabel userLabel = new JLabel("Cos: " + (screenSize.getWidth() * x + screenSize.getHeight()*y)/((Math.sqrt(Math.pow(screenSize.getWidth(),2)+Math.pow(screenSize.getHeight(),2)))*(Math.sqrt(Math.pow(x,2)+Math.pow(y,2)))));
        userPaneCos.add(userLabel, BorderLayout.WEST);


        JPanel userPaneSin = new JPanel(new BorderLayout());
        JLabel userLabe2 = new JLabel("Sin:  " + Math.sqrt(1-((screenSize.getWidth() * x + screenSize.getHeight()*y)/((Math.sqrt(Math.pow(screenSize.getWidth(),2)+Math.pow(screenSize.getHeight(),2)))*(Math.sqrt(Math.pow(x,2)+Math.pow(y,2)))))));
        userPaneSin.add(userLabe2, BorderLayout.WEST);


        JPanel panel = new JPanel(new BorderLayout());
        panel.add(userPaneCos, BorderLayout.NORTH);
        panel.add(userPaneSin, BorderLayout.CENTER);
        JPanel panel2 = new JPanel(new BorderLayout());
        panel2.add(userLabeX, BorderLayout.NORTH);
        panel2.add(userLabeY, BorderLayout.CENTER);
        content.add(panel, BorderLayout.NORTH);
        content.add(panel2, BorderLayout.CENTER);


        JButton play = new JButton("PLAY");
        JButton stop = new JButton("STOP");
        JButton left = new JButton("|<<");
        JButton right = new JButton(">>|");
        JButton pause = new JButton("PAUSE");


        Box box = new Box(BoxLayout.X_AXIS);


//        box = Box.createHorizontalBox();
        box.add(left);
        box.add(play);
        box.add(pause);
        box.add(stop);
        box.add(right);

        content.add(box, BorderLayout.SOUTH);

        play.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < 500; i++) {
                    frame.setLocation(x, y);
                    x += (int) (100 * Math.cos(angle)) >> 1;
                    y += (int) (50 * Math.sin(angle)) >> 1;


                    frame.setLocation(x, y);
                    frame.setVisible(false);

                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }

                    angle += 0.1;
                    frame.setVisible(true);
                    try {
                        Thread.sleep(1250);
                    } catch (InterruptedException e2) {
                        e2.printStackTrace();
                    }
                }
            }
        });
        stop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setLocation(x=460, y=170);
            }
        });
        left.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < 1; i++) {
                    frame.setLocation(x, y);
                    angle -= 0.2;
                    x -= (int) (100 * Math.cos(angle)) >> 1;
                    y -= (int) (50 * Math.sin(angle)) >> 1;


                    frame.setLocation(x, y);
                    frame.setVisible(false);

                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }


                    frame.setVisible(true);
                    try {
                        Thread.sleep(1250);
                    } catch (InterruptedException e2) {
                        e2.printStackTrace();
                    }
                }
            }
        });
        right.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < 1; i++) {
                    frame.setLocation(x, y);
                    angle += 0.1;
                    x += (int) (100 * Math.cos(angle)) >> 1;
                    y += (int) (50 * Math.sin(angle)) >> 1;


                    frame.setLocation(x, y);
                    frame.setVisible(false);

                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }


                    frame.setVisible(true);
                    try {
                        Thread.sleep(1250);
                    } catch (InterruptedException e2) {
                        e2.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }}
            }
        });
        pause.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(true);
            }
        });
        frame.setIconImage(img);
        frame.pack();


        frame.getDefaultCloseOperation();
        frame.setLocation(x, y);
        frame.setVisible(true);

    }


}
